<template>
  <div class="container">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card style="margin-top: 10px">
      <!-- 搜索 -->
      <el-row type="flex">
        <el-input
          style="width: 30%"
          v-model="params.query"
          placeholder="请输入内容,根据名称搜索"
          clearable
          @clear="getGoodsList"
        >
          <template>
            <el-button icon="el-icon-close"></el-button>
          </template>
          <template #append>
            <el-button icon="el-icon-search" @click="search"></el-button>
          </template>
        </el-input>
        <el-button
          style="margin-left: 20px"
          type="primary"
          size="small"
          @click="$router.push('/addgoods')"
          >添加商品</el-button
        >
      </el-row>
      <!-- 表格 -->
      <el-row style="margin-top: 15px">
        <el-table :data="goodsList" border size="small">
          <el-table-column type="index" align="center"></el-table-column>
          <el-table-column label="商品名称" prop="goods_name"></el-table-column>
          <el-table-column
            label="商品价格"
            prop="goods_price"
            width="100px"
            align="center"
          ></el-table-column>
          <el-table-column
            label="商品重量"
            prop="goods_weight"
            width="100px"
            align="center"
          ></el-table-column>
          <el-table-column
            label="创建时间"
            prop="add_time"
            width="180px"
            align="center"
          >
            <template #default="{ row }">
              {{ row.add_time | formatTime }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200px" align="center">
            <template #default="{ row }">
              <el-button
                icon="el-icon-edit"
                type="primary"
                size="small"
                @click="$router.push(`/addgoods?goods_id=${row.goods_id}`)"
              ></el-button>
              <el-button
                icon="el-icon-delete"
                type="danger"
                size="small"
                @click="delGoods(row.goods_id)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <!-- 分页 -->
      <el-row style="margin-top: 10px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="params.total"
          :current-page="params.pagenum"
          :page-size="params.pagesize"
          :page-sizes="[5, 10, 15, 20]"
        >
        </el-pagination>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { goods, delGoods } from '@/api/goods'
export default {
  name: 'goods',
  filters: {
    formatTime (val) {
      return dayjs(val).format('YYYY-MM-DD hh:mm:ss')
    }
  },
  data () {
    return {
      goodsList: [],
      params: {
        pagenum: 1, // 当前页码
        pagesize: 10, // 每页显示条数
        total: 0, // 总数
        query: '' // 关键字
      }
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList () {
      const res = await goods(this.params)
      this.goodsList = res.goods
      this.params.total = res.total
      this.$message.success('获取商品列表成功')
    },
    handleSizeChange (newSize) {
      this.params.pagesize = newSize
      this.getGoodsList()
    },
    handleCurrentChange (newPage) {
      this.params.pagenum = newPage
      this.getGoodsList()
    },
    async delGoods (id) {
      try {
        await this.$confirm('此操作将永久删除改商品,是否继续', {
          type: 'warning'
        })
        await delGoods(id)
        this.getGoodsList()
      } catch (err) {
        console.log(err)
      }
    },
    search () {
      if (this.params.query.trim() === '') {
        this.$message('请输入关键字')
      } else {
        this.params.pagenum = 1
        this.getGoodsList()
      }
    }
  }
}
</script>

<style scoped>
.container {
  padding: 20px;
  background-color: #f5f7f8;
}
</style>
